package com.kehongborui.servletinitparam;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief
 * @email 122162005@qq.com
 * @date 2018-07-17
 */
@WebServlet("/loginSuccess")
public class LoginSuccessServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        Cookie[] cookies = req.getCookies(); // 获取所有cookies
        String username = null;  // cookie
        for (Cookie cookie : cookies) {
            cookie.setValue("11111");
            // 从cookie中获取用户名
            if ("username".equals(cookie.getName())) {
                username = cookie.getValue();
            }
        }

        resp.getWriter().println("<h1>" + username + "，登录成功</h1>");
    }
}
